/*
 * PromptForArgs.cs
 * 
 * Form to prompt for arguments, in absence of command line.
 */
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;

namespace Jitsu
{
    internal class PromptForArgs : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textboxFilespec;
        private System.Windows.Forms.CheckBox checkboxRecursion;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textboxPattern;
        private System.Windows.Forms.CheckBox checkboxReplacement;
        private System.Windows.Forms.TextBox textboxReplacement;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.CheckBox checkboxIgnoreCase;
        private System.Windows.Forms.CheckBox checkboxSingeline;
        private System.Windows.Forms.CheckBox checkboxMimicVS;
        private System.Windows.Forms.Button buttonOK;
        private System.Windows.Forms.Button buttonCancel;

        public PromptForArgs(Options options)
        {
            // Required for Windows Form Designer support
            InitializeComponent();

            // Load a decent icon.
            // this.Icon = new Icon(typeof(Global), "RSaR.ico");

            // Display version number in titlebar.
            this.Text += string.Format(" (v{0})", this.GetType().Assembly.GetName().Version);

            // Hold a reference to the settings object.
            this.m_options = options;

            // Init control states.
            this.textboxFilespec.Text = String.Empty;

            this.checkboxRecursion.Checked = options.DoRecursion;
            this.checkboxIgnoreCase.Checked = options.RegexIgnoreCase;
            this.checkboxSingeline.Checked = options.RegexSingleline;
            this.checkboxMimicVS.Checked = options.RegexMimicVS;

            this.textboxPattern.Text = options.RegexPattern;
            this.textboxReplacement.Text = options.ReplacementText;
            if (options.ReplacementText != null)
                this.checkboxReplacement.Checked = true;

            // Load last user settings.
            InitializeFromIsolatedStorage();
        }

        //
        // Implementation

        private Options m_options;

        private void InitializeFromIsolatedStorage()
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly())
            using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("RsarUserSettings.xml", System.IO.FileMode.OpenOrCreate, isf))
            {
                System.Xml.XmlDocument xmldoc = new XmlDocument();
                try
                {
                    xmldoc.Load(isfs);

                    this.textboxFilespec.Text = xmldoc.SelectSingleNode(@"/UserSettings/LastFilePattern").InnerText;
                    this.textboxPattern.Text = xmldoc.SelectSingleNode(@"/UserSettings/LastRegexPattern").InnerText;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
        }

        private void PersistToIsolatedStorage()
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly())
            using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("RsarUserSettings.xml", System.IO.FileMode.Create, isf))
            {
                System.Xml.XmlTextWriter xtw = new XmlTextWriter(isfs, System.Text.Encoding.Default);

                xtw.WriteStartDocument();
                xtw.WriteStartElement("UserSettings");
                xtw.WriteStartElement("LastFilePattern");
                xtw.WriteString(this.textboxFilespec.Text);
                xtw.WriteEndElement();
                xtw.WriteStartElement("LastRegexPattern");
                xtw.WriteString(this.textboxPattern.Text);
                xtw.WriteEndElement();
                xtw.WriteEndElement();
                xtw.WriteEndDocument();

                xtw.Close();
            }
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.buttonOK = new System.Windows.Forms.Button();
            this.buttonCancel = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.textboxFilespec = new System.Windows.Forms.TextBox();
            this.textboxPattern = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.checkboxRecursion = new System.Windows.Forms.CheckBox();
            this.checkboxIgnoreCase = new System.Windows.Forms.CheckBox();
            this.checkboxSingeline = new System.Windows.Forms.CheckBox();
            this.checkboxMimicVS = new System.Windows.Forms.CheckBox();
            this.textboxReplacement = new System.Windows.Forms.TextBox();
            this.checkboxReplacement = new System.Windows.Forms.CheckBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonOK
            // 
            this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.buttonOK.Location = new System.Drawing.Point(526, 347);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(71, 24);
            this.buttonOK.TabIndex = 8;
            this.buttonOK.Text = "OK";
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            // 
            // buttonCancel
            // 
            this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonCancel.Location = new System.Drawing.Point(602, 347);
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.Size = new System.Drawing.Size(72, 24);
            this.buttonCancel.TabIndex = 9;
            this.buttonCancel.Text = "Cancel";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(201, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "&Files to search through:";
            // 
            // textboxFilespec
            // 
            this.textboxFilespec.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textboxFilespec.Location = new System.Drawing.Point(8, 26);
            this.textboxFilespec.Name = "textboxFilespec";
            this.textboxFilespec.Size = new System.Drawing.Size(668, 20);
            this.textboxFilespec.TabIndex = 1;
            this.textboxFilespec.Text = "textboxFilespec";
            this.textboxFilespec.TextChanged += new System.EventHandler(this.textbox_TextChanged);
            // 
            // textboxPattern
            // 
            this.textboxPattern.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textboxPattern.Location = new System.Drawing.Point(8, 78);
            this.textboxPattern.Name = "textboxPattern";
            this.textboxPattern.Size = new System.Drawing.Size(668, 20);
            this.textboxPattern.TabIndex = 4;
            this.textboxPattern.Text = "textboxPattern";
            this.textboxPattern.TextChanged += new System.EventHandler(this.textbox_TextChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 61);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(201, 15);
            this.label2.TabIndex = 3;
            this.label2.Text = "Regex &pattern:";
            // 
            // checkboxRecursion
            // 
            this.checkboxRecursion.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.checkboxRecursion.CheckAlign = System.Drawing.ContentAlignment.TopRight;
            this.checkboxRecursion.Location = new System.Drawing.Point(514, 52);
            this.checkboxRecursion.Name = "checkboxRecursion";
            this.checkboxRecursion.Size = new System.Drawing.Size(162, 17);
            this.checkboxRecursion.TabIndex = 2;
            this.checkboxRecursion.Text = "Recurse &subfolders";
            this.checkboxRecursion.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // checkboxIgnoreCase
            // 
            this.checkboxIgnoreCase.Location = new System.Drawing.Point(8, 26);
            this.checkboxIgnoreCase.Name = "checkboxIgnoreCase";
            this.checkboxIgnoreCase.Size = new System.Drawing.Size(440, 20);
            this.checkboxIgnoreCase.TabIndex = 0;
            this.checkboxIgnoreCase.Text = "&Ignore case";
            // 
            // checkboxSingeline
            // 
            this.checkboxSingeline.Location = new System.Drawing.Point(8, 43);
            this.checkboxSingeline.Name = "checkboxSingeline";
            this.checkboxSingeline.Size = new System.Drawing.Size(440, 20);
            this.checkboxSingeline.TabIndex = 1;
            this.checkboxSingeline.Text = "Single-&line semantics (ie: \".\" includes the newline char)";
            // 
            // checkboxMimicVS
            // 
            this.checkboxMimicVS.Location = new System.Drawing.Point(8, 61);
            this.checkboxMimicVS.Name = "checkboxMimicVS";
            this.checkboxMimicVS.Size = new System.Drawing.Size(440, 20);
            this.checkboxMimicVS.TabIndex = 2;
            this.checkboxMimicVS.Text = "Mimic &VS (\":i\" for identifier, \":n\" for numeric literal, \":q\" for string literal" +
                ")";
            // 
            // textboxReplacement
            // 
            this.textboxReplacement.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textboxReplacement.Enabled = false;
            this.textboxReplacement.Location = new System.Drawing.Point(8, 130);
            this.textboxReplacement.Name = "textboxReplacement";
            this.textboxReplacement.Size = new System.Drawing.Size(668, 20);
            this.textboxReplacement.TabIndex = 6;
            this.textboxReplacement.Text = "textboxReplacement";
            // 
            // checkboxReplacement
            // 
            this.checkboxReplacement.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.checkboxReplacement.Location = new System.Drawing.Point(8, 113);
            this.checkboxReplacement.Name = "checkboxReplacement";
            this.checkboxReplacement.Size = new System.Drawing.Size(201, 17);
            this.checkboxReplacement.TabIndex = 5;
            this.checkboxReplacement.Text = "&Replacement string:";
            this.checkboxReplacement.TextAlign = System.Drawing.ContentAlignment.TopLeft;
            this.checkboxReplacement.CheckedChanged += new System.EventHandler(this.checkboxReplacement_CheckedChanged);
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox1.Controls.Add(this.checkboxIgnoreCase);
            this.groupBox1.Controls.Add(this.checkboxMimicVS);
            this.groupBox1.Controls.Add(this.checkboxSingeline);
            this.groupBox1.Location = new System.Drawing.Point(8, 165);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(668, 94);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Regex options";
            // 
            // PromptForArgs
            // 
            this.AcceptButton = this.buttonOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.buttonCancel;
            this.ClientSize = new System.Drawing.Size(684, 383);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.checkboxReplacement);
            this.Controls.Add(this.textboxReplacement);
            this.Controls.Add(this.textboxPattern);
            this.Controls.Add(this.textboxFilespec);
            this.Controls.Add(this.checkboxRecursion);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.buttonCancel);
            this.Controls.Add(this.buttonOK);
            this.Name = "PromptForArgs";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Regex Search and Rescue";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private void textbox_TextChanged(object sender, EventArgs e)
        {
            // The filespec and pattern textboxes are required fields.
            if (this.textboxFilespec.Text.Length == 0 || this.textboxPattern.Text.Length == 0)
                this.buttonOK.Enabled = false;
            else
                this.buttonOK.Enabled = true;
        }

        private void checkboxReplacement_CheckedChanged(object sender, System.EventArgs e)
        {
            this.textboxReplacement.Enabled = this.checkboxReplacement.Checked;
        }

        private void buttonOK_Click(object sender, System.EventArgs e)
        {
            // Apply the settings.
            string text = this.textboxFilespec.Text;
            string baseDirectory = Path.GetDirectoryName(text);
            string filePattern = Path.GetFileName(text);

            if (baseDirectory.Length > 0)
                m_options.BaseDirectory = baseDirectory;
            else
                m_options.BaseDirectory = Environment.CurrentDirectory;

            if (filePattern.Length > 0)
                m_options.FilePattern = filePattern;
            else
                m_options.FilePattern = "*";

            m_options.DoRecursion = this.checkboxRecursion.Checked;
            m_options.RegexIgnoreCase = this.checkboxIgnoreCase.Checked;
            m_options.RegexSingleline = this.checkboxSingeline.Checked;
            m_options.RegexMimicVS = this.checkboxMimicVS.Checked;

            m_options.RegexPattern = this.textboxPattern.Text;

            if (this.checkboxReplacement.Checked)
                m_options.ReplacementText = this.textboxReplacement.Text;

            // Save the settings.
            this.PersistToIsolatedStorage();

            // Close and return.
            this.DialogResult = DialogResult.OK;
        }
    }
}